package controller.library
{
	import events.library.EventResolveLibraryConflict;
	import events.library.EventUpdateLibraryResource;
	
	import model.ModelLibrary;
	import model.ModelLibraryConflicts;
	
	import org.flashIso.engine.library.objects.LBase;
	import org.flashIso.engine.library.objects.LBinaryData;
	import org.flashIso.engine.library.objects.LImage;
	import org.robotlegs.mvcs.Command;
	
	public class CommandResolveLibraryConflict extends Command
	{
		[Inject]
		public var mlc:ModelLibraryConflicts;
		
		[Inject]
		public var ml:ModelLibrary;
		
		[Inject]
		public var e:EventResolveLibraryConflict;
		
		private function updateItem(item:LBinaryData) : void{
			if (item is LImage){
				dispatch(new EventUpdateLibraryResource(EventUpdateLibraryResource.UPDATE_LIBRARY_RESOURCE, item.id, item.data, EventUpdateLibraryResource.TYPE_IMAGE));
			}else
			if (item is LBinaryData){
				dispatch(new EventUpdateLibraryResource(EventUpdateLibraryResource.UPDATE_LIBRARY_RESOURCE, item.id, item.data, EventUpdateLibraryResource.TYPE_BINARY));
			}
		}
		private function cancelItem(item:LBase) : void {
			if (item is LImage){
				mlc.conflicImages.removeItemAt(mlc.conflicImages.getItemIndex(item));
			}else
				if (item is LBinaryData){
					mlc.conflictBinaries.removeItemAt(mlc.conflictBinaries.getItemIndex(item));
				}
		}
		private function renameItem(item:LBinaryData) : void {
			var i:uint = 1;
			if (item is LImage){
				while (ml.getLImageByID(item.id+"_" + i) != null){
					i++;
				}
				item.id = item.id+"_" + i;
				ml.addLImage(item as LImage);
			}else
			if (item is LBinaryData){
				while (ml.getLBinaryDataByID(item.id+"_" + i) != null){
					i++;
				}
				item.id = item.id+"_" + i;
				ml.addLBinaryData(item as LBinaryData);
			}
		}
		override public function execute() : void {
			var i:uint;
			switch(e.resolution)
			{
				case EventResolveLibraryConflict.RESOLUTION_UPDATE:
				{
					updateItem(e.item);
					cancelItem(e.item);
					break;
				}
				case EventResolveLibraryConflict.RESOLUTION_UPDATE_ALL:
				{
					for (i = 0; i < mlc.conflicImages.length; i++){
						updateItem(mlc.conflicImages.getItemAt(i) as LImage);
					}
					for (i = 0; i < mlc.conflictBinaries.length; i++){
						updateItem(mlc.conflictBinaries.getItemAt(i) as LBinaryData);
					}
					mlc.conflicImages.removeAll();
					mlc.conflictBinaries.removeAll();
					break;
				}
				case EventResolveLibraryConflict.RESOLUTION_RENAME:
				{
					renameItem(e.item);
					cancelItem(e.item);
					break;
				}
				case EventResolveLibraryConflict.RESOLUTION_RENAME_ALL:
				{
					for (i = 0; i < mlc.conflicImages.length; i++){
						renameItem(mlc.conflicImages.getItemAt(i) as LImage);
					}
					for (i = 0; i < mlc.conflictBinaries.length; i++){
						renameItem(mlc.conflictBinaries.getItemAt(i) as LBinaryData);
					}
					mlc.conflicImages.removeAll();
					mlc.conflictBinaries.removeAll();
					break;
				}
				case EventResolveLibraryConflict.RESOLUTION_CANCEL:
				{
					cancelItem(e.item);
					break;
				}
				case EventResolveLibraryConflict.RESOLUTION_CANCEL_ALL:
				{
					mlc.conflicImages.removeAll();
					mlc.conflictBinaries.removeAll();
					break;
				}
			}
		}
		
	}
}